using System;
using System.Windows.Forms;
using FBroSharp;
using FBroSharp.Const;
using FBroSharp.DataType;
using FBroSharp.Event;
using FBroSharp.Lib;
using FBroSharp.Value;

namespace 自动登录浏览器
{
    /// <summary>
    /// 浏览器事件处理类 - MANDATORY for browser creation
    /// ⚠️ CRITICAL: 必须继承自 FBroSharpBrowserEvent (不是 FBroEvent!)
    /// </summary>
    public class BrowserEvent : FBroSharpBrowserEvent
    {
        private Form1 _form;

        public BrowserEvent(Form1 form)
        {
            _form = form;
        }

        /// <summary>
        /// ⚠️ REQUIRED: Browser created (TWO parameters!)
        /// </summary>
        public override void OnAfterCreated(IFBroSharpBrowser browser, IFBroSharpDictionaryValue extrainfo)
        {
            Console.WriteLine("✅ 浏览器创建成功");
            _form.UpdateStatus("浏览器已就绪");
            
            // 触发自动登录流程
            _form.BeginAutoLogin();
        }

        /// <summary>
        /// ⚠️ CRITICAL FOR SINGLE-WINDOW APPS: Popup control - Prevent new windows
        /// </summary>
        public override bool OnBeforePopup(
            IFBroSharpBrowser browser, 
            IFBroSharpFrame frame, 
            string target_url, 
            string target_frame_name,
            FBroSharpWindowOpenDisposition target_disposition, 
            bool user_gesture, 
            FBroSharpPopupfeatures popupFeatures, 
            ref FBroSharpWindowsInfo windowInfo,
            ref FBroSharpBrowserSetting settings,
            ref bool no_javascript_access, 
            IFBroSharpUseExtraData user_settings)
        {
            // 在当前窗口打开链接，阻止弹出新窗口
            Console.WriteLine($"阻止弹窗: {target_url}");
            frame.LoadURL(target_url);
            return true;  // true = 阻止弹窗
        }

        /// <summary>
        /// ⚠️ REQUIRED: Address changed
        /// </summary>
        public override void OnAddressChange(IFBroSharpBrowser browser, IFBroSharpFrame frame, string url)
        {
            if (frame.IsMain())
            {
                _form.UpdateUrl(url);
                Console.WriteLine($"地址变更: {url}");
            }
        }

        /// <summary>
        /// ⚠️ REQUIRED: Title changed
        /// </summary>
        public override void OnTitleChange(IFBroSharpBrowser browser, string title)
        {
            _form.UpdateTitle(title);
            Console.WriteLine($"标题变更: {title}");
        }

        /// <summary>
        /// ⚠️ REQUIRED: Loading state changed
        /// </summary>
        public override void OnLoadingStateChange(IFBroSharpBrowser browser, bool isLoading, bool canGoBack, bool canGoForward)
        {
            _form.UpdateLoadingState(isLoading, canGoBack, canGoForward);
            
            if (isLoading)
            {
                Console.WriteLine("⏳ 页面加载中...");
            }
            else
            {
                Console.WriteLine("✅ 页面加载完成");
            }
        }

        /// <summary>
        /// ⚠️ REQUIRED: Page load completed
        /// </summary>
        public override void OnLoadEnd(IFBroSharpBrowser browser, IFBroSharpFrame frame, int httpStatusCode)
        {
            if (frame.IsMain())
            {
                _form.UpdateStatus($"加载完成 (HTTP {httpStatusCode})");
                Console.WriteLine($"✅ 页面加载完成: HTTP {httpStatusCode}");
            }
        }

        /// <summary>
        /// ⚠️ REQUIRED: Page load error (errorCode is int!)
        /// </summary>
        public override void OnLoadError(IFBroSharpBrowser browser, IFBroSharpFrame frame, 
            int errorCode, string errorText, string failedUrl)
        {
            if (frame.IsMain())
            {
                _form.UpdateStatus($"加载失败: {errorText}");
                Console.WriteLine($"❌ 页面加载失败: {errorText} (错误代码: {errorCode})");
                
                MessageBox.Show(
                    $"页面加载失败！\n\n错误: {errorText}\nURL: {failedUrl}\n错误代码: {errorCode}",
                    "加载错误",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Warning
                );
            }
        }

        /// <summary>
        /// ⚠️ REQUIRED: Before browser close
        /// </summary>
        public override void OnBeforeClose(IFBroSharpBrowser browser)
        {
            Console.WriteLine("🔒 浏览器即将关闭");
            _form.UpdateStatus("浏览器已关闭");
        }

        /// <summary>
        /// ⚠️ REQUIRED: Can browser close
        /// </summary>
        public override bool DoClose(IFBroSharpBrowser browser)
        {
            Console.WriteLine("❓ 检查浏览器是否可以关闭");
            return false; // false = 允许关闭
        }
    }
}

